-
Notifications
You must be signed in to change notification settings - Fork 522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(example): add react example #1076
Conversation
wow @codesuki I ❤️ so much. Thanks! I cloned this locally and I'm trying to get the devserver to work. Both at your commit and the one I've pushed to your branch, I can
fails there at that last line. Does it have anything to do with the |
03141de
to
bd8a6b7
Compare
Hey, thanks for improving the PR! It's much cleaner now. I didn't know what to do about the tests. I just left for Hawaii vacation so I cannot check the code now. I guess you get a null pointer exception? Thanks for working on these rules! |
|
bd8a6b7
to
b589571
Compare
Okay, problem here was
I've added another commit here to make the test pass - but we don't really want the Let's say you're not using Bazel. It seems that bundling React into your app with Rollup requires this workaround where you list some symbols in the rollup.config. Seems not desirable. |
Does the bundle work when using Ah, the named exports you mention fixes this right? I agree, having to adjust rollup.config.js is cumbersome. Anyhow, what is the benefit of using I haven't done any web dev for several years now so I can't say anything useful to whether or not react should be bundled or loaded at runtime. Correct me if I say something wrong. For our actual production case, we don't mind either way I think. (I know this is just about the example here) |
@codesuki @alexeagle thanks for your work on this! Any thoughts on how this may work with a CRA application? |
Hey - this is really cool! Some (JS/node) stylistic notes:
'use strict';
const domino = require('domino');
const fs = require('fs');
const vm = require('vm');
describe('react webapp', () => {
let sandbox;
beforeAll(() => {
// Domino gives us enough of the DOM API that we can run our JavaScript in node rather than the
// browser. That makes this test a lot faster
const html = fs.readFileSync(require.resolve('./index.html'));
const window = domino.createWindow(html, '/');
// Make all Domino types available as types in the sandbox.
sandbox = vm.createContext({
window,
document: window.document,
navigator: window.navigator,
process: {
env: 'production',
},
});
});
it('works', () => {
const bundlePath = require.resolve('./bundle.es2015');
const bundleContents = fs.readFileSync(bundlePath, 'utf8');
vm.runInContext(bundleContents, sandbox, { filename: bundlePath });
expect(sandbox.document.body.textContent.trim()).toEqual('Hello from React!');
});
}); |
Lets wait till the new rollup rule has landed, there's a couple of different rollup configs/plugins I'd like to try out here which may make things a bit easier, which will be possible with the new rule. @jkrems On your point 3. while I fundamentally agree that sandboxing code is a good idea, I don't think it fits unit tests. |
We tried following this approach and it worked OK for getting react working with a It would be helpful if the example included some of these common libraries, since they seem to be sensitive to how things are connected. Specifically, when looking in the browser: react-router.umd.js includes
and
|
What I wrote is pretty much how Jest works. It hides it behind some nice facade but this is pretty close to the exact behavior (down to the node APIs used). It's not very common to do this setup in individual test files, that's definitely true. :) But if your test is using |
@alexeagle I think this can be closed now |
@mrmeku why is that? |
FYI |
This Pull Request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs in two weeks. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs! |
This PR was automatically closed because it went two weeks without a reply since it was labeled "Can Close?" |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Example for react
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
There are 3 big blockers for react to be easily usable.
#1045
#555
#933
I added workarounds for both so the example works with devserver and prodserver (bundle)